home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 3298 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.3 KB  |  60 lines

  1. Path: newsfeed.tip.net!dataphone!newsmaster
  2. From: skorpio@dataphone.se (Jarmo Paavilainen)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Can a function return a (pointer to a) function?
  5. Date: 27 Jan 1996 17:50:22 GMT
  6. Organization: Dataphone Communication Networks
  7. Message-ID: <4edoku$ljk@nic.dataphone.se>
  8. NNTP-Posting-Host: nikson.dataphone.se
  9. X-Newsreader: NeoLogic News for OS/2 [version: 4.2]
  10.  
  11. In message <4ebaaa$jh6@barnacle.iol.ie> - "John D. Hourihane" <hourihaj@iol.ie>
  12.  writes:
  13.  
  14. Im not sure but try this :
  15. #include <stdio.h>
  16.  
  17. #define ADD        0
  18. #define MULTIPLY    1
  19.  
  20. int add(int x, int y);
  21. int multiply(int x, int y);
  22.  
  23. (int (*)(int,int)) pick(int s);
  24.  
  25. int main()
  26. {
  27. void (*func)(int,int);
  28.  
  29. func = pick(ADD);
  30. printf("5 + 4 = %d\n",func(5,4));
  31. func = pick(MULTIPLY);
  32. printf("5 * 4 = %d\n",func(5,4));
  33. return 0;
  34. }
  35.  
  36. int add(int x, int y)
  37. { return x + y; }
  38.  
  39. int multiply(int x, int y)
  40. { return x * y; }
  41.  
  42. (int (*)(int,int)) pick(int s)
  43. {
  44. if (s == ADD)
  45.     return add;
  46. return multiply;
  47. }
  48.  
  49. As I wrote, Im not sure, if this will work !
  50.  
  51. Take care,
  52. Skorpio
  53. _________________________________________________________________________
  54.  Jarmo Paavilainen, Skorpio  | e-mail : skorpio@dataphone.se
  55.  Sweden                      | home   : http://www.dataphone.se/~skorpio
  56.  
  57. Spelling ? I don't need no stinking spelling !
  58.  
  59.  
  60.